﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infratructure.Interfares;
using B.S.BaseData.Write.Api.Application.Command.MenuCommand;
using MediatR;

namespace B.S.BaseData.Write.Api.Application.CommandHander.MenuCommand
{
    public class UpdateMenuCommandHander : IRequestHandler<UpdateMenuCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Menu> baseRequsitory;

        public UpdateMenuCommandHander(IMapper mapper, IBaseRequsitory<Menu> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        /// <summary>
        /// 修改菜单处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(UpdateMenuCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var entity = mapper.Map<Menu>(request);
            entity.CreateBy = request.CreateBy;
            entity.UpdateDate = DateTime.Now.ToString();

            var list = baseRequsitory.UpdateAsync(entity).Result;

            if (list > 0)
            {
                result.Code = ApiEnum.修改成功;
                result.Msg = ApiEnum.修改成功.ToString();
                result.Data = list;
            }
            else
            {
                result.Code = ApiEnum.修改失败;
                result.Msg = ApiEnum.修改失败.ToString();
                result.Data = list;
            }

            return Task.FromResult(result);
        }
    }
}
